{include file="public/header" /}
<body class="gray-bg">
<div class="wrapper wrapper-content">
    <div class="ibox-title">
        <h5>绑定合伙人</h5>
    </div>
    <div class="ibox-content layui-form">
        <div class="layui-row layui-col-space10 layui-form-item ">
            <input type="hidden" name="id" value="{$par_id}">
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                <label class="layui-form-label">待绑定用户：</label>
                <div class="layui-input-inline">
                    <select name="user" lay-verify="required" lay-search="">
                        <option value="">请选择用户</option>
                        {if !empty($user)}
                        {foreach name="user" item="vo"}
                        <option value="{$vo.user_id}" >{$vo.user_phone}</option>
                        {/foreach}
                        {/if}
                    </select>
                </div>
                <div class="layui-input-inline">
                    <input type="text" name="now" lay-verify="required|percent" class="layui-input percent" placeholder="收益比例" >
                </div>
                <div class="layui-form-mid layui-word-aux">% （剩余可分配<span id="last">{:abs(array_sum(array_column ($par,'par_percent')) - 100)}</span>%）</div>
            </div>
            {notempty name="$par"}
            {foreach $par as $vo}
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                <label class="layui-form-label">已绑定用户：</label>
                <input type="hidden" name="par_id[]" value="{$vo.par_id}">
                <div class="layui-input-inline">
                    <input type="text" value="{$vo.user_phone}"  class="layui-input layui-disabled" disabled>
                </div>
                <div class="layui-input-inline">
                    <input type="text" name="percent[]" lay-verify="required|percent" class="layui-input percent" placeholder="收益比例" value="{$vo.par_percent}">
                </div>
                <div class="layui-form-mid layui-word-aux">%</div>
            </div>
            {/foreach}
            {/notempty}
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="component-form-element">保存
                    </button>
                    <button class="layui-btn layui-btn-primary" onclick="history.go(-1)">返回</button>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="public/footer" /}
<script>
    $(".percent").bind("input propertychange",function () {
        var percent = 0;
        $.each($('.percent'),function (index,item) {
            if(!isNaN(parseFloat($(item).val()))){
                percent += parseFloat($(item).val());
            }
        })
        $("#last").text(100-percent > 0 ? 100-percent : 0);
    })

    layui.use(['form'], function() {
        var form = layui.form;

        form.verify({
            percent: [
                /^[0-9]+(.[0-9]{1,2})?$/
                ,'保留1~2位小数的正实数'
            ]
        });

        form.on('submit(component-form-element)', function (data) {
            var percent = 0;
            $.each($('.percent'),function (index,item) {
                percent += parseFloat($(item).val());
            })
            if(percent != 100){
                wk.error('收益比例总数必须等于100%');
                return false;
            }

            $('.layui-btn').addClass('layui-disabled').attr('disabled','disabled');
            $.ajax({
                url:"{:url('partnerAdd')}",
                type:'post',
                dataType:'json',
                data:data.field,
                success:function(res){
                    if (res.code == 200) {
                        wk.success(res.msg,"{:url('partner/index')}")
                    } else {
                        wk.error(res.msg,'','$(".layui-btn").removeClass(\'layui-disabled\').removeAttr(\'disabled\');');
                    }
                }
            })
        });
    });
</script>
</body>
</html>